package com.lsabcd.dubbo.test;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.springframework.util.StringUtils;

@Activate(group = {CommonConstants.CONSUMER})
public class IpFilter implements Filter {

    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        String ip = RpcContext.getContext().getAttachment("ip");
        if (!StringUtils.isEmpty(ip)) {
            IpHelper.setClientIp(ip);
        } else {
            RpcContext.getContext().setAttachment("ip", IpHelper.getClientIp());
        }
        return invoker.invoke(invocation);
    }
}
